package Control;

import java.sql.Date;
import GUI.ActivityReportOutputGui;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import Entity.ActivityReport;
import Entity.Parformance_report;
import Entity.Tardiness_report;

import java.util.List;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JOptionPane;
import javax.xml.crypto.Data;

import com.sun.org.apache.xerces.internal.impl.xpath.regex.ParseException;

import Entity.MsgPackage;
import GUI.ParformanceReportGui;
import GUI.ReportPanelGUI;
import GUI.ScreenManager;
import Entity.ActivityReport;
import GUI.TardinessReportGuiOutput;
public class ReportControl {
	private ParformanceReportGui PR_output;
	private TardinessReportGuiOutput TRG_output;
	private ActivityReportOutputGui G_ARoutput;
	private ActivityReport AR;
	private ReportPanelGUI disReport;
	//private String[] disReportArry; // [0] = start , [1] = end
    private MsgPackage disReoptMeg;
	private String start_date ;
	private String end_date;
	private Tardiness_report TR;
	private Parformance_report PR;

	
   public ReportControl(){
	  PR_output = new ParformanceReportGui() ;
		 disReport = new ReportPanelGUI();
		 PR = new Parformance_report();
		 AR = new ActivityReport();
		 TR = new Tardiness_report();
		TRG_output = new TardinessReportGuiOutput() ;
		G_ARoutput = new ActivityReportOutputGui();
	}
   /**
	 * 
	 * @param opcode that will send to the data base 
	 * 
	 * @return true if the user entered all the parameters or false else
	 */
	public boolean ActivityReport_pressed(OP_CODE opcode){
		 	
		start_date = disReport. getStartdateListener().getText();
		if (start_date == null)
		{
			
			JOptionPane.showMessageDialog(null, "Start Date is missing", "ERROR", JOptionPane.ERROR_MESSAGE);

             return false;
		}
		end_date = disReport.getEndDateListener().getText();
		if(end_date == null)
		{
			JOptionPane.showMessageDialog(null, "End Date is missing", "ERROR", JOptionPane.ERROR_MESSAGE);
		        return false;	
		}
		else{
		AR.setFrom(start_date);
		AR.setTo(end_date);
		
		        disReoptMeg=new MsgPackage();
	        	disReoptMeg.setObject(AR); //convert the start and end date to object
		        disReoptMeg.setOp_code(opcode);//get the op-code to know which query to active
		        ScreenManager.getConnection().accept(disReoptMeg);//send the msg to the server
		        fillActivityreport(AR);
	            return true;
		}
	}
	/**
	 * 
	 * @param opcode that will send to the data base 
	 * 
	 * @return true if the user entered all the parameters or false else
	 */
		public boolean TardinessReport_pressed(OP_CODE opcode){
		 	
			start_date = disReport. getStartdateListener().getText();
			if (start_date == null)
			{
				
				JOptionPane.showMessageDialog(null, "Start Date is missing", "ERROR", JOptionPane.ERROR_MESSAGE);
	             return false;
			}
			end_date = disReport.getEndDateListener().getText();
			if(end_date == null)
			{
				JOptionPane.showMessageDialog(null, "End Date is missing", "ERROR", JOptionPane.ERROR_MESSAGE);
			        return false;	
			}
			else{
			TR.setFrom(start_date);
			TR.setTo(end_date);
			
			        disReoptMeg=new MsgPackage();
		        	disReoptMeg.setObject(TR); //convert the start and end date to object
			        disReoptMeg.setOp_code(opcode);//get the op-code to know which query to active
			        ScreenManager.getConnection().accept(disReoptMeg);//send the msg to the server
			        fillTardinessReport(TR);
		            return true;
			}
			
        	
 	}
		/**
		 * 
		 * @param opcode that will send to the data base 
		 * 
		 * @return true if the user entered all the parameters or false else
		 */

		public boolean ParformanceReport_pressed(OP_CODE opcode){
		 	
			start_date = disReport. getStartdateListener().getText();
			if (start_date == null)
			{
				
				JOptionPane.showMessageDialog(null, "Start Date is missing", "ERROR", JOptionPane.ERROR_MESSAGE);
	             return false;
			}
			end_date = disReport.getEndDateListener().getText();
			if(end_date == null)
			{
				JOptionPane.showMessageDialog(null, "End Date is missing", "ERROR", JOptionPane.ERROR_MESSAGE);
			        return false;	
			}
			else{
			PR.setFrom(start_date);
			PR.setTo(end_date);
			
			        disReoptMeg=new MsgPackage();
		        	disReoptMeg.setObject(PR); //convert the start and end date to object
			        disReoptMeg.setOp_code(opcode);//get the op-code to know which query to active
			        ScreenManager.getConnection().accept(disReoptMeg);//send the msg to the server
			        fillParformanceReport(PR);
		            return true;
			}
			
        	
 	}
	/**
	 * 
	 * @param pR2 the Variable of the Entity  Performance Report
	 * set the 
	 */
	
      
		public void fillParformanceReport(Parformance_report pR2) {
			// TODO Auto-generated method stub
			
			int i = PR.getSumExtendedApp();
			String strI = String.valueOf(i);
			PR_output.getTextField_appDay().setText(strI);
			
			int i2 = 15;
			String str2 = String.valueOf(i2);
			PR_output.getTextField_MedianA().setText(str2);
			
			int i3 = 17;
			String str3 = String.valueOf(i3);
			PR_output.getTextField_DeviationA().setText(str3);
			
			int i4 = 	PR.getSumReturnStageDur();
			String str4 = String.valueOf(i4);
			PR_output.getTextField_addDay().setText(str4) ;
			
			int i5 = 19;
			String str5 = String.valueOf(i5);
			PR_output.getTextField_MedianB().setText(str5);
			
			int i6 = 8;
			String str6 = String.valueOf(i3);
			PR_output.getTextField_DeviationB().setText(str6);
			
		}
		public void fillTardinessReport(Tardiness_report tR2) {
			TRG_output.getTable().setValueAt(FrequencyDistribution(TR.getSum_late_requests()),2 , 2);
			TRG_output.getTable().setValueAt(FrequencyDistribution(TR.getNumber_late_requests()),1 , 2);
			
			
			
			
			TRG_output.getTable().setValueAt(median(TR.getSum_late_requests()), 2, 3);
			TRG_output.getTable().setValueAt(median(TR.getNumber_late_requests()),1 , 3);
			

			TRG_output.getTable().setValueAt(StandardDeviation(TR.getSum_late_requests()), 2, 3);
			TRG_output.getTable().setValueAt(StandardDeviation(TR.getNumber_late_requests()),1 , 3);
			
		}
		public void fillActivityreport(ActivityReport aR2) {
        	G_ARoutput.getTable_1().setValueAt(FrequencyDistribution(AR.getActiveReqList()), 1, 2);
        	G_ARoutput.getTable_1().setValueAt(FrequencyDistribution(AR.getFrozenReqList()), 2, 2);
        	G_ARoutput.getTable_1().setValueAt(FrequencyDistribution(AR.getClosedReqList()), 3, 2);
        	G_ARoutput.getTable_1().setValueAt(FrequencyDistribution(AR.getRejectReqList()), 4, 2);
		    
        	G_ARoutput.getTable_1().setValueAt(StandardDeviation(AR.getActiveReqList()), 1, 3);
        	G_ARoutput.getTable_1().setValueAt(StandardDeviation(AR.getFrozenReqList()), 2, 3);
        	G_ARoutput.getTable_1().setValueAt(StandardDeviation(AR.getClosedReqList()), 3, 3);
        	G_ARoutput.getTable_1().setValueAt(StandardDeviation(AR.getRejectReqList()), 4, 3);
        	
        	G_ARoutput.getTable_1().setValueAt(median(AR.getActiveReqList()), 1, 4);
        	G_ARoutput.getTable_1().setValueAt(median(AR.getFrozenReqList()), 2, 4);
        	G_ARoutput.getTable_1().setValueAt(median(AR.getClosedReqList()), 3, 4);
        	G_ARoutput.getTable_1().setValueAt(median(AR.getRejectReqList()), 4, 4);
        	
        	G_ARoutput.getTable_1().setValueAt(AR.getActiveReqDuration(),1,1);
        	G_ARoutput.getTable_1().setValueAt(AR.getActiveReqDuration(),2,1);
        	G_ARoutput.getTable_1().setValueAt(AR.getActiveReqDuration(),3,1);
        	G_ARoutput.getTable_1().setValueAt(AR.getActiveReqDuration(),4,1);
	}
		public void displayReport_pressed2(OP_CODE opcode){
        	
        	disReoptMeg.setOp_code(opcode);
        }
	
	
	
	
	
	
	//*******************************************************************************************//
		/**
		 * This class makes all calculation needed
		 * to display on one of the report's panel
		 *  
		 */
	
	/**
	 * 
	 * @param med the arr that will returned from the data base white all the data
	 * @return the median of the data
	 */
	
	public double median(ArrayList<Integer> med) 
	{

	    float middle = med.size()/2; 
	  		
	  		//sort the list
	  		Collections.sort(med);
	   

	    if (middle%2 == 1) 
	    {
	        return med.get((int)middle);
	    } 
	    else 
	    {
	        return (double) ((med.get((int)middle) + med.get((int)middle+1) ) / 2.0);
	    }
	}
	/**
	 * 
	 * @param  avr array list that hold the  data from the data base
	 * @return the avrage of the avr 
	 */
	
	public double avrage(ArrayList<Integer> avr) 
	{
	    int sum = 0;
	    
	    for (int i = 0; i < avr.size(); i++) 
	    	
	            sum = sum + avr.get(i);
	    
	    return (double)sum / avr.size();
	}
	
	/**
	 * 
	 * @param sd array list that hold the data from the data base
	 * @return the standardfevition of the data
	 */
	public double StandardDeviation(ArrayList<Integer>sd){
		
		int num = 1/sd.size();    
		System.out.println(num);
		
		double avre,temp=0;
		
		avre = avrage(sd);
		
		for(int i=0;i<sd.size();i++)
		{
			temp = temp + Math.pow(sd.get(i)- avre,2);
		}
		
	
		return Math.sqrt(num*temp) ;
		
		
	}
	/**
	 * 
	 * @param fdis array list that hold the data from the data base
	 * @return Frequency Distribution of the data
	 */
	public double FrequencyDistribution(ArrayList<Integer>fdis){
		
		int sum = 0;
		for( int i=0 ;i<fdis.size();i++) {
		    sum += i;
		}

		
		return (sum/fdis.size());
		
	}

///////////////////////////////get/set/////////////////////////////////////////////
	public ReportPanelGUI getDisReport() {
		return disReport;
	}
	public void setDisReport(ReportPanelGUI disReport) {
		this.disReport = disReport;
	}
	
	public MsgPackage getDisReoptMeg() {
		return disReoptMeg;
	}
	public void setDisReoptMeg(MsgPackage disReoptMeg) {
		this.disReoptMeg = disReoptMeg;
	}
	public ActivityReport getAR() {
		return AR;
	}
	public void setAR(ActivityReport aR) {
		AR = aR;
	}
	public String getStart_date() {
		return start_date;
	}
	public void setStart_date(String start_date) {
		this.start_date = start_date;
	}
	public String getEnd_date() {
		return end_date;
	}
	public void setEnd_date(String end_date) {
		this.end_date = end_date;
	}
	public TardinessReportGuiOutput getTRG_output() {
		return TRG_output;
	}
	public void setTRG_output(TardinessReportGuiOutput tRG_output) {
		TRG_output = tRG_output;
	}
	public ActivityReportOutputGui getG_ARoutput() {
		return G_ARoutput;
	}
	public void setG_ARoutput(ActivityReportOutputGui g_ARoutput) {
		G_ARoutput = g_ARoutput;
	}
	public Tardiness_report getTR() {
		return TR;
	}
	public void setTR(Tardiness_report tR) {
		TR = tR;
	}

	
	
}
